import flash.geom.Transform;

var collabFlag:Boolean;
var collabSignal:Boolean;
var collabAdminMode:String;
var currentSpeaker:String;

function renderPortlet(slot)
{
	trace("renderPortlet: "+slot);
	
	var mainBg=mlf.gatekeeper.native.Caller.scanElement(slot, "conBg");
	var containerFilter:flash.filters.DropShadowFilter=new flash.filters.DropShadowFilter(10,90,0x000000,80,35,35,0.5,2);
	var bgFilterArr:Array=new Array();
	bgFilterArr.push(containerFilter);
	mainBg.filters=bgFilterArr;

	mainBg._alpha=80;

	var location=mlf.gatekeeper.native.Caller.scanElement(slot, "location");
	var locationFilter=new flash.filters.DropShadowFilter();
	/*for (var a in locationFilter)
	{
		trace(a+": "+locationFilter[a]);
	}*/
	locationFilter.color=0xffffff;
	var locFilterArr:Array=new Array();
	locFilterArr.push(locationFilter);
	location.filters=locFilterArr;
}

function highlightBlock(target:MovieClip)
{
	// trace("highlightBlock: "+target);
	
	var borderColor:Number=0x99defd;
	var fillColorStart:Number=0xf0f8fd;
	var fillColorEnd:Number=0xd5effc;

	// target._alpha=100;
	
	target.lineStyle(0, borderColor);
	target.moveTo(0, 0);
	var colors=[fillColorStart, fillColorEnd];
	var alphas=[100, 100];
	var ratios=[0, 0xff];
	var matrix={matrixType:"box", x:100, y:0, w:300, h:16, r:(90/180)*Math.PI};
	target.beginGradientFill("linear", colors, alphas, ratios, matrix);
	target.lineTo(target._width, 0);
	target.lineTo(target._width, target._height);
	target.lineTo(0, target._height);
	target.lineTo(0, 0);
	target.endFill();
}

function highlightBG(target:MovieClip, cover:MovieClip, alpha:Number)
{
	// trace("highlightBG "+target+" "+cover+" "+alpha);
	cover._x=target._x;
	cover._y=target._y;
	cover._alpha=alpha;
}

function renderTextStyle(slot, ins, fmt, dropColor)
{
	var target=mlf.gatekeeper.native.Caller.scanElement(slot, ins);
	target.setTextFormat(fmt);
	var targetFilter=new flash.filters.DropShadowFilter();
	targetFilter.color=dropColor;
	var tarFilterArr:Array=new Array();
	tarFilterArr.push(targetFilter);
	target.filters=tarFilterArr;
}

function messageHint(slot:String, content:String, type:String, callbackObj:Object)
{
	var pCacheCallbackObj=callbackObj;
	var messTf=mlf.gatekeeper.native.Caller.scanElement(slot, "messageHint");
	var cloud=mlf.gatekeeper.native.Caller.scanElement(slot, "cloud");
	trace("cloud: "+cloud);
	cloud.onPress=null;
	if (callbackObj!=null)
	{
		cloud.onPress=function()
		{
			Packages.system.MLF.invoke(pCacheCallbackObj);
		}
	}
	var hintCloud=cloud.media_cloud.hintCloud;
	messTf.htmlText=content;
	if (type=="warning")
	{
		hintCloud.gotoAndStop(2);
	}
	else if (type=="hint")
	{
		hintCloud.gotoAndStop(3);
	}
	else hintCloud.gotoAndStop(1);
}

function focusManager()
{
	var focusListener:Object=new Object();
	focusListener.onSetFocus=function(oldFocus_txt, newFocus_txt) 
	{
		if (mlf.mcmanager.PathResolver.drawPath(oldFocus_txt).indexOf("tf_txt")>-1)
		{
			oldFocus_txt.borderColor=0x50565b;
			oldFocus_txt.background=false;
		}
		if (mlf.mcmanager.PathResolver.drawPath(newFocus_txt).indexOf("tf_txt")>-1)
		{
			newFocus_txt.borderColor=0x99defd;
			newFocus_txt.background=true;
			newFocus_txt.backgroundColor=0xffffff;
		}
	};
	Selection.addListener(focusListener);
}

function displayHint(param, data, slot)
{
	// var target=mlf.mcmanager.PathResolver.extractPath(param);
	var hintLayer=mlf.gatekeeper.native.Caller.scanElement(slot, "hintLayer");
	var falseTarget=hintLayer.createEmptyMovieClip("falseTarget", hintLayer.getNextHighestDepth());
	falseTarget.lineStyle(0, 0x000000);
	falseTarget.moveTo(0, 0);
	falseTarget.lineTo(2, 0);
	falseTarget.lineTo(2, 2);
	falseTarget.lineTo(0, 2);
	falseTarget.lineTo(0, 0);
	falseTarget.endFill();
	falseTarget._x=_root._xmouse;
	falseTarget._y=_root._ymouse;
	falseTarget._alpha=0;
	var hintInstance=hintLayer.createEmptyMovieClip("hintInstance", hintLayer.getNextHighestDepth());
	var eleArr=param.split("-");
	var content=Packages.system.MLF.scanDictionary(hintLayer, "Hint_"+eleArr[eleArr.length-1]);
	Packages.system.Util.createHint(falseTarget, content, hintInstance);
}

function disposeHint(param, data, slot)
{
	Packages.system.Util.removeHint();
	var hintLayer=mlf.gatekeeper.native.Caller.scanElement(slot, "hintLayer");
	hintLayer.hintInstance.removeMovieClip();
	hintLayer.falseTarget.removeMovieClip();
}

focusManager();

function launchCollab()
{
	trace("launchCollab...");
	collabFlag=true;
	collabSignal=true;
	Packages.system.MLF.addListener("onEvtListInfo", this);
	Packages.system.Collab.startEngine("test");
	// Packages.system.Collab.register(Packages.task.Composition.username);

}

function stopCollab()
{
	Packages.system.Collab.stopEngine();
	collabFlag=false;
}

function onEvtListInfo(evtObj)
{
	 trace(this._name+".onEvtListInfo: "+evtObj.param.length);
	nameList=evtObj.param;
	var oldList=Packages.task.Composition.accessList();
	Packages.task.Composition.updateList(nameList);
	refreshPersonList(oldList, nameList);
}

function refreshPersonList(oldList:Array, userList:Array)
{
	trace("refreshPersonList: "+userList.length);
	var container=Packages.system.MLF.scanElement("PersonList", "container");
	for (var j=0; j<oldList.length; j++)
	{
		container[oldList[j]].removeMovieClip();
		mlf.mcmanager.component.IndexSlot.removeElement("PersonList", oldList[j]);
	}
	for (var i=0; i<userList.length; i++)
	{
		var personML2:String="";
		personML2+="<moonlight name='PersonList' template='PersonList-container' uri='app/PersonList.xml'>";
		personML2+="<portlet name='"+userList[i]+"' drag='no' style='no'>";
		personML2+="<textfield x='5' name='item' width='100' height='20' html='true' selectable='no'>"+userList[i]+"</textfield>";
		personML2+="<textfield x='110' name='activity_"+userList[i]+"' width='20' height='20' html='true' selectable='no'>100</textfield>";
		personML2+="<textfield x='140' name='delay_"+userList[i]+"' width='50' height='20' html='true' selectable='no'>0</textfield>";
		personML2+="<hotspot name='spot_"+userList[i]+"' x='5' y='3' height='20' width='100' alpha='0' />";
		personML2+="<handler ins='PersonList-spot_"+userList[i]+"' type='onPress' value='spot' src='lib:notes.system.Platform.changeSpeaker' />";
		personML2+="</portlet>";
		personML2+="</moonlight>";

		mlf.gatekeeper.native.Caller.MLoader(null, null, null, null, personML2);
	}
}

function changeSpeaker(param)
{
	trace("changeSpeaker: "+param);
	var eleArr=param.split("-");
	var member=eleArr[eleArr.length-1].substr(5);
	trace("member name: "+member);

	if (member!=currentSpeaker && currentSpeaker==Packages.task.Composition.username)
	{
		Packages.modula.MicrophoneDevice.closePub();
		Packages.modula.MicrophoneDevice.subscribe(collabAdminMode);
	}

	var args=new Array();
	args.push(member);
	Packages.system.Collab.updateInfo(this, "acceptChangeSpeaker", args);
}

function acceptChangeSpeaker(speaker:String)
{
	trace("*acceptChangeSpeaker: "+speaker);
	// trace("*collabAdminMode: "+collabAdminMode);
	/*if (speaker!=Packages.task.Composition.username)
	{
		// Packages.modula.MicrophoneDevice.closePub();
		// Packages.modula.MicrophoneDevice.subscribe(collabAdminMode);
	}
	else */
	if (speaker==Packages.task.Composition.username)
	{
		Packages.modula.MicrophoneDevice.unsubscribe(collabAdminMode);
		Packages.modula.MicrophoneDevice.publish(collabAdminMode, "append", true);
	}

	currentSpeaker=speaker;
	trace("currentSpeaker: "+currentSpeaker);
}

function reportActivityLevel(activityLevel)
{
	var args=new Array();
	args.push(Packages.task.Composition.username);
	args.push(activityLevel);
	Packages.system.Collab.updateInfo(this, "acceptActivityLevel", args);
}

function acceptActivityLevel(username, activityLevel)
{
	var memberActivity=Packages.system.MLF.scanElement("PersonList", "activity_"+username);
	memberActivity.text=activityLevel;
}